<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        if (!isset($_POST['submit'])) {
            $this->display('./Public/admin/login.html');
        } else {
            if ($_POST['username']=='admin' && $_POST['password']=='admin') {
                session('admin_username', $_POST['username']);
                $this->success('login', 'home');
            } else {
                $this->error('Invaid username or password');
            }
        }

        //$this->success('login', U('home'));
    }

    public function home() {
        if (!session('?admin_username')) {
            $this->error('Please login!', 'index');
        }
        if ($this->isAjax()) {
            $this->ajaxReturn($this->fetch('./Public/admin/home.html'),'EVAL');
        } else {
            $this->assign('content',  $this->fetch('./Public/admin/home.html'));
            $this->display('./Public/admin/manage.html');
        }
//        $path1 = U('EditNews/index');
//        $path2 = U('EditActivity/index');
//        $this->show('<a href="'.$path1.'">添加news</a>
//                     <a href="'.$path2.'">添加activity</a>');
    }

    public function news($id=0) {
        if (!session('?admin_username')) {
            $this->error('Please login!', 'index');
        }
        $news = D('News');
        if ($id!=0) {
            $news->delete($id);
        }
        $list = $news->field('id,title,type')->select();
        $this->assign('list', $list);
        if ($this->isAjax()) {
            $this->ajaxReturn($this->fetch('./Public/admin/news.html'),'EVAL');
        } else {
            $this->assign('content',  $this->fetch('./Public/admin/news.html'));
            $this->display('./Public/admin/manage.html');
        }
    }

    public function activity($id=0) {
        if (!session('?admin_username')) {
            $this->error('Please login!', 'index');
        }
        $activity = D('Activity');
        if ($id!=0) {
            $activity->delete($id);
        }
        $list = $activity->field('id,title')->select();
        $this->assign('list', $list);
        if ($this->isAjax()) {
            $this->ajaxReturn($this->fetch('./Public/admin/activity.html'),'EVAL');
        } else {
            $this->assign('content',  $this->fetch('./Public/admin/activity.html'));
            $this->display('./Public/admin/manage.html');
        }
    }

    public function charity($id=0) {
        if (!session('?admin_username')) {
            $this->error('Please login!', 'index');
        }
        $c = D('Charity');
        if ($id!=0) {
            $c->delete($id);
        }
        $list = $c->field('id,title')->select();
        $this->assign('list', $list);
        if ($this->isAjax()) {
            $this->ajaxReturn($this->fetch('./Public/admin/charity.html'),'EVAL');
        } else {
            $this->assign('content',  $this->fetch('./Public/admin/charity.html'));
            $this->display('./Public/admin/manage.html');
        }
    }

    public function game($id=0) {
        if (!session('?admin_username')) {
            $this->error('Please login!', 'index');
        }
        $game = D('Game');
        if ($id!=0) {
            $game->delete($id);
        }
        $list = $game->select();
        $this->assign('list', $list);
        if ($this->isAjax()) {
            $this->ajaxReturn($this->fetch('./Public/admin/game.html'),'EVAL');
        } else {
            $this->assign('content',  $this->fetch('./Public/admin/game.html'));
            $this->display('./Public/admin/manage.html');
        }
    }

    public function resource($id=0) {
        if (!session('?admin_username')) {
            $this->error('Please login!', 'index');
        }
        $resource = D('Resource');
        if ($id!=0) {
            $res = $resource->where("id=$id")->find();
            if ($resource->delete($id)) {
                unlink('./Public'.$res['path']);
            }
        }
        $list = $resource->select();
        $this->assign('list', $list);
        if ($this->isAjax()) {
            $this->ajaxReturn($this->fetch('./Public/admin/resource.html'),'EVAL');
        } else {
            $this->assign('content',  $this->fetch('./Public/admin/resource.html'));
            $this->display('./Public/admin/manage.html');
        }
    }

    private function isAjax() {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }
}